package com.zookeeper.basic.two.zkclient;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;


/**
 * Created by 58 on 2016-7-17.
 * Zkclient获取节点数据信息
 */
public class ZkClientGetData {
    public static void main(String[] args) throws InterruptedException {
        String path = "/zk-book554";
        ZkClient zkClient = new ZkClient("127.0.0.1:2181",5000);
        zkClient.createEphemeral(path,"123222");
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            //节点发生变化之后，服务端会发送事件通知
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("Node "+dataPath+" changed,new data "+data);
            }

            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("Node "+dataPath+" deleted");
            }
        });
        System.out.println(zkClient.readData(path).toString());
        zkClient.writeData(path,"12463454");
        Thread.sleep(1000);
        zkClient.delete(path);
        Thread.sleep(Integer.MAX_VALUE);

    }
}
